package com.test.util;

import com.alibaba.fastjson.JSON;
import com.test.entities.FiddlerTemplate;
import com.test.entities.PerformanceTestReport;
import org.apache.commons.lang.StringEscapeUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ReportData {

    public static String genReport4PerformanceTestReport(List<PerformanceTestReport> alist) {
        String result = "";
        StringBuilder sb = new StringBuilder();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

        sb.append("<html>");
        sb.append("<head>");
        sb.append("<title>性能测试报告"+df.format(new Date())+"</title>");
        sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        //样式内容
        sb.append("<style type=\"text/css\">");
        sb.append(".tablename table th {background:#8FBC8F}");
        sb.append(".tablename table tr{ background:#FAEBD7;text-align:center}");
        sb.append("</style></head>");
        //样式结尾
        //主体部分一个div
        sb.append("<div class=\"tablename\">");
        //一个table
        sb.append("<table width=\"1000\"  height=\"100\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse:collapse;\">");
        sb.append("<th>序号</th><th>渠道</th><th>接口/页面名称</th><th>环境</th><th>压测时间</th><th>并发数</th>");
        sb.append("<th>总请求数</th><th>平均响应时间（ms）</th><th>错误数</th><th>错误率(%)</th><th>实际TPS</th><th>理论TPS</th>");
        sb.append("<th>开始时间</th><th>结束时间</th><th>服务实例数</th><th>测试用户数</th><th>测试结论</th><th>测试是否通过</th><th>备注</th>");

        for(int i = 0 ; i < alist.size() ; i++) {
            sb.append("<tr><td>"+alist.get(i).getId()+"</td><td>"+alist.get(i).getChannel()+"</td><td>"+alist.get(i).getApi()+"</td>" +
                    "<td>"+alist.get(i).getEnvironment()+"</td><td>"+alist.get(i).getDuration()+"</td><td>"+alist.get(i).getThreadNum()+"</td>");
            sb.append("<td>"+alist.get(i).getTotalRequestNum()+"</td><td>"+alist.get(i).getAverageReponseTime()+"</td>" +
                    "<td>"+alist.get(i).getErrorNum()+"</td><td>"+alist.get(i).getErrorRate()+"</td><td>"+alist.get(i).getPracticeTps()+"</td>" +
                    "<td>"+alist.get(i).getTheoryTps()+"</td>");
            sb.append("<td>"+alist.get(i).getStartTime()+"</td><td>"+alist.get(i).getEndTime()+"</td><td>"+alist.get(i).getServerNum()+"</td>" +
                    "<td>"+alist.get(i).getTesterNum()+"</td><td>"+alist.get(i).getTestConclusion()+"</td>" +
                    "<td>"+alist.get(i).getTestResult()+"</td><td>"+alist.get(i).getRemark()+"</td></tr>");
        }

        sb.append("</table>");


        //sb.append(detailHtml);

        //table与html结尾
        sb.append("</div></body></html>");
        new SendMail().send(sb.toString(),"性能测试报告"+df.format(new Date()));
        return sb.toString();
    }



    public static String genReport(List<FiddlerTemplate> alist) {
        String result = "";
        StringBuilder sb = new StringBuilder();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式

        int totalTestcases = alist.size();
        int successTestCases = 0;
        int failureTestCsases = 0;

        StringBuffer detailHtml = new StringBuffer();
        String shortResponseData = "";
        detailHtml.append("<th>编号</th><th>请求地址</th><th>请求方法</th><th>content-type</th><th>请求头数据</th><th>返回结果</th><th>测试结果</th>");
        for (int i = 0; i < alist.size(); i++) {
//            if(alist.get(i).getResponseData().length() > 20){
//                shortResponseData = alist.get(i).getResponseData().substring(0,20);
//            }else{
//                shortResponseData = alist.get(i).getResponseData();
//            }
            if(Util.isJSONValid(alist.get(i).getResponseData())){
                shortResponseData = alist.get(i).getResponseData();
            } else {
                shortResponseData = StringEscapeUtils.unescapeHtml(alist.get(i).getResponseData());
            }

            if (alist.get(i).getFinalTestResult().equals("测试通过")) {
                successTestCases++;
//                detailHtml.append("<tr><td>" + alist.get(i).getId() + "</td><td>" + alist.get(i).getFullUrl() + "</td><td>" +
//                        alist.get(i).getRequestMethod() + "</td><td>" + alist.get(i).getContentType() + "</td>" +
//                        "<td>" + alist.get(i).getHead() + "</td> <td>" + shortResponseData+ "</td><td>" + alist.get(i).getFinalTestResult()+ "</td>");

            } else {
                failureTestCsases++;
                detailHtml.append("<tr  style=\"color: #FF0000\"><td>" + alist.get(i).getId() + "</td><td>" + alist.get(i).getFullUrl() + "</td><td>" +
                        alist.get(i).getRequestMethod() + "</td><td>" + alist.get(i).getContentType() + "</td>" +
                        "<td>" + alist.get(i).getHead() + "</td> <td>" + shortResponseData+ "</td><td>" + alist.get(i).getFinalTestResult()+ "</td>");
            }


        }

//        PrintStream printStream = null;
//        try {
//            printStream = new PrintStream(new FileOutputStream("report.html"));//路径默认在项目根目录下
//        } catch (
//                Exception e) {
//            e.printStackTrace();
//        }

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        String resultRate = numberFormat.format((float) successTestCases / (float) totalTestcases * 100);

        sb.append("<html>");
        sb.append("<head>");
        sb.append("<title>测试报告的样式</title>");
        sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        //样式内容
        sb.append("<style type=\"text/css\">");
        sb.append(".tablename table th {background:#8FBC8F}");
        sb.append(".tablename table tr{ background:#FAEBD7;text-align:center}");
        sb.append("</style></head>");
        //样式结尾
        //主体部分一个div
        sb.append("<div class=\"tablename\">");
        //一个table
        sb.append("<table width=\"500\"  height=\"100\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse:collapse;\">");
        sb.append("<th>用例集/测试方法</th><th>总计</th><th>通过</th><th>失败</th><th>通过率</th><tr>");
        sb.append("<td>接口测试集合</td><td>" + totalTestcases + "</td><td>" + successTestCases + "</td><td>" + failureTestCsases + "</td><td>" + resultRate + "" +
                "%</td> </tr>");
        sb.append("</table><br>");
        sb.append("<table width=\"800\"  height=\"100\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse:collapse;\">");
//        sb.append("<th>测试方法</th><th>测试项</th><th>测试模块</th><th>测试描述</th><th>验证点</th>");
//        sb.append("<tr><td>" + 12333 + "</td><td>变量2</td><td>变量3</td><td>变量4</td><td>变量5</td>");

        sb.append(detailHtml);


        sb.append("</tr></table>");
        //table与html结尾
        sb.append("</div></body></html>");
        //printStream.println(sb.toString());
        //System.out.println(sb.toString());
        new SendMail().send(sb.toString(),"接口测试报告"+df.format(new Date()));
        return sb.toString();
    }
}
